package com.ccx.coachsite.ui.login;

import com.ccx.coachsite.bus.Account;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.ccx.coachsite.bus.GetVerifyCodeBus;
import com.ccx.coachsite.bus.RepasswordBus;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.R;

public class RepasswordActivity extends CommonActivity {

	private View mSave; // 保存

	private EditText mOldPassword; // 旧密码
	private EditText mNewPassword; // 新密码
	private EditText mRetryPwd; // 确认密码
	private EditText mVerityCode; // 验证码
	private View mVerifyBtn; // 获取验证
	
	private RepasswordBus bo;
	
	private Account account;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	

	@Override
	protected void initComponents() {
		mOldPassword = (EditText) findViewById(R.id.old_pwd);
		mNewPassword = (EditText) findViewById(R.id.new_pwd);
		mRetryPwd = (EditText) findViewById(R.id.retry_pwd);
		mVerityCode = (EditText) findViewById(R.id.repassword_verify_code);
		mVerifyBtn = findViewById(R.id.repassword_btn_verify);
		mVerifyBtn.setOnClickListener(new OnGetVerify());
		
		mSave = findViewById(R.id.repassword_btn);
		mSave.setOnClickListener(new OnSave());
	}
	
	public void setEmpty()
	{
		mOldPassword.setText("");
		mNewPassword.setText("");
		mRetryPwd.setText("");
		mVerityCode.setText("");
	}
	
	class OnGetVerify implements OnClickListener {
		@Override
		public void onClick(View v) {
			String mobilenumber = account.getMobile();
			GetVerifyCodeBus bo = new GetVerifyCodeBus(RepasswordActivity.this);
			bo.getVerifyCode(mobilenumber);
		}
	}

	/**
	 * 发送变更请求
	 */
	private void sendRequest() {
		if (!checkPwd())
			return;

		if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
			Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
			return;
		}

		if (bo == null) {
			bo = new RepasswordBus(RepasswordActivity.this);
		}

		final String newPwd = mNewPassword.getText().toString();
		final String oldPwd = mOldPassword.getText().toString();
		final String code = mVerityCode.getText().toString();
		bo.changePassword(account, newPwd, oldPwd,code);
	}

	class OnSave implements OnClickListener {
		@Override
		public void onClick(View v) {
			sendRequest();
		}
	}

	/**
	 * 密码是否合法
	 * @return
	 */
	private boolean checkPwd() {
		Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);

		String oldPwdStr = mOldPassword.getText().toString().trim();
		String newPwdStr = mNewPassword.getText().toString().trim();
		String retryPwdStr = mRetryPwd.getText().toString().trim();
		String verifyCode = mVerityCode.getText().toString().trim();
		
		if (StringUtil.isEmpty(oldPwdStr)) {
			CommonViewUtils.showErrorByEditText(mOldPassword, getString(R.string.change_pwd_empty), shake);
			return false;
		}
		
		/*
		if (!oldPwdStr.equals(account.getPassword())) {
			CommonViewUtils
					.showErrorByEditText(mOldPassword, getString(R.string.change_pwd_old_pwd_wrong), shake);
			return false;
		}
		 */
		if (StringUtil.isEmpty(newPwdStr)) {
			CommonViewUtils
					.showErrorByEditText(mNewPassword, getString(R.string.change_pwd_new_pwd_empty), shake);
			return false;
		}

		if (!StringUtil.isPwdValid(newPwdStr)) {
			CommonViewUtils.showErrorByEditText(mNewPassword, getString(R.string.change_pwd_valid), shake);
			return false;
		}

		if (StringUtil.isEmpty(retryPwdStr)) {
			CommonViewUtils.showErrorByEditText(mRetryPwd, getString(R.string.change_pwd_retry_pwd_empty), shake);
			return false;
		}

		if (!newPwdStr.equals(retryPwdStr)) {
			CommonViewUtils
					.showErrorByEditText(mRetryPwd, getString(R.string.change_pwd_retry_pwd_nosame), shake);
			return false;
		}
		
		if (StringUtil.isEmpty(verifyCode)) {
			CommonViewUtils.showErrorByEditText(mVerityCode, getString(R.string.register_verify_code_empty),
					shake);
			return false;
		}

		return true;
	}


	@Override
	protected int getMainContentViewId() {
		return R.layout.repassword_activity;
	}


	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}


	@Override
	protected void setCustomTitleTop() {
		TopViewUtil.updateTitle(this, R.id.login_top_title, R.string.change_pwd);
		//TopViewUtil.updateRightTitle(this, R.id.login_top_right, R.string.change_pwd_save);
		//TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.login_save_btn);
	}


	@Override
	protected void initData() 
	{
		account = new Account();
	}
}
